library(sf)
## Linking to GEOS 3.5.1, GDAL 2.1.3, proj.4 4.9.2, lwgeom 2.3.2 r15302
library(raster)
## Loading required package: sp
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:raster':
## 
##     intersect, select, union
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(mapview)
## Loading required package: leaflet
library(RColorBrewer)
data('cookfarm', package = "GSIF")

names(cookfarm)
## [1] "readings"    "profiles"    "bdensity"    "grids"       "weather"    
## [6] "proj4string"
grids <- cookfarm$grids %>% 
  dplyr::select(x, y, DEM, TWI, Cook_fall_ECa, Cook_spr_ECa) %>% 
  rasterFromXYZ(crs = cookfarm$proj4string)

plot(grids)

profiles <- cookfarm$profiles %>% 
  st_as_sf(coords = c('Easting', 'Northing'), crs = cookfarm$proj4string)

plot(profiles)

# Plot points
mapview(profiles)
mapview(profiles, zcol = "BLD")
mapview(profiles, zcol = "TAXSUSDA")
pal_continuous <- colorRampPalette(brewer.pal(7, "BrBG"))
pal_categorical <- colorRampPalette(brewer.pal(9, "Set1"))

mapview(profiles, zcol = "BLD", col.regions = pal_continuous, legend = TRUE)
mapview(profiles, zcol = "TAXSUSDA", col.regions = pal_categorical, legend = TRUE)
# Plot grids
mapview(grids)
mapview(grids, col.regions = pal_continuous, legend = TRUE)
mapview(grids$TWI, col.regions = pal_continuous, legend = TRUE)
# Plot both together
mapview(grids$DEM, col.regions = pal_continuous, legend = TRUE) + mapview(profiles, zcol = "TAXSUSDA", col.regions = pal_categorical)
# Burst to separate soil classes
mapview(profiles, zcol = "TAXSUSDA", col.regions = pal_categorical, legend = TRUE, burst = TRUE)
# Syncing several maps
m1 <- mapview(grids$DEM)
m2 <- mapview(grids$TWI)
m3 <- mapview(profiles)

sync(m1, m2, m3)
# Raster specific functions
viewRGB(poppendorf)
viewRGB(poppendorf, 4, 3, 2)
# plainview
plainview(poppendorf[[1]])
plainview(poppendorf[[1]], at = seq(8000, 11000, 50))
plainview(poppendorf, 4, 3, 2)
plainview(poppendorf, 5, 4, 3, quantiles = c(0.5, 1))
# slideview
slideview(poppendorf[[1]], poppendorf[[5]])
slideview(poppendorf[[1]], poppendorf[[5]], legend = FALSE)
img1 <- poppendorf[[1]]
img2 <- poppendorf[[5]]

slideview(img1, img2,
          label1 = "Poppendorf-Layer-1",
          label2 = "Poppendorf-Layer-2")
# cubeview
# stck <- raster::stack("tappelhans/uni/talks/jena_201703/data/barazar.tif")
# raster::nlayers(stck) * raster::ncell(stck) # takes a while!!
# cubeview(stck)
mapview(
  profiles, 
  popup = popupImage('https://www.vcard.wur.nl/WebServices/GetMedia.ashx?id=37263')
)

Advanced mapview

library(xts)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Attaching package: 'xts'
## The following objects are masked from 'package:dplyr':
## 
##     first, last
library(dygraphs)

profiles$SOURCEID <- as.character(profiles$SOURCEID)

records <- cookfarm$readings
records$SOURCEID <- as.character(records$SOURCEID)

ids <- unique(records$SOURCEID)

# Subset sensors
ids <- sample(ids, size = 5) 
  
idx_sensors <- which(profiles$SOURCEID %in% ids)
sensors <- profiles[idx_sensors,]

make_ts <- function(id) {
  
  records %>% 
    filter(SOURCEID == id) %>% 
    dplyr::select(-SOURCEID) %>%
    dplyr::select(Date, ends_with('VW')) %>% 
    xts(.$Date)
}

make_dygraph <- function(id){
  ts <- make_ts(id)
  dygraph(ts)
} 

l_graphs <- lapply(
  ids,
  make_dygraph
)
make_dygraph(ids[1])
mapview(sensors, popup = popupGraph(graphs = l_graphs, width = 300, height = 300))